/*  Parse.cpp - Parsing cmdlet for Primer3 formatted output.
 *
 *  It finds how many prime #'s are in the file, and then will
 *  display the last prime and its index number.
 *
 *  Syntax: "parse *insert_prime_log_text_file's_name_here*"
 *
 *  ~Xevious 2-18-09
 */
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

fstream log;

int main(int argc, char *argv[])
{
    system("title Parsing primes log file...");
    unsigned long count = 0;
    bool error = false;
    string linefeed, finalprime;  // string for line feed.
    log.open(argv[1]);  // open file passed as parameter.
    log.seekg(ios::beg);  // seek to beginning of file.
    if (log.is_open())  // once the file is open...
    {
        do
        {
            //system("cls");
            log >> linefeed;  // reads file until whitespace.
            if (linefeed != "Prime")  // precedes the final prime number in my
            {                         // Primer3 app's output formatting.
                count += 1;  // increment the number of primes parsed.
                cout << /*"#" <<*/ count << " ";  // used for showing current count.
            }
            /*else if (linefeed == "Prime")  // this was just to tell me when it was found.
            {
                cout << "\tlinefeed found prime\n";
            }*/
        } while (linefeed != "Prime");  // do all that until it finds "Prime"
       for (int i = 1; i <= 3; ++i)  // loop for my Primer3 formatted prime output,
        {                            // this will put the final prime # into a variable
            log >> finalprime;       // for displaying in the console.
        }
    }
    else { cout << "\nFile read error.\n"; error = true; }
    log.close();
    if (!error)
    {
        count += 1;  // increment to include final prime (shown after "Prime")
        cout << "\n\nFound " << count << " prime numbers in log file." << endl;
        cout << "The last prime (index #" << count << " ) = \"" << finalprime << "\"" << endl;  // print results
    }
    return error;
}
